﻿using ChongJu.ModelManager.PLC;
using Public.Logger;
using Public.Tools;
using System;
using System.Collections.Generic;

namespace ChongJu.GlobalInfo
{
    public static class PLCAddress
    {
        /// <summary>
        /// 开始/停止地址
        /// </summary>
        public static PlcAddressModel StartStop { get; private set; }

        /// <summary>
        /// 等级地址
        /// </summary>
        public static PlcAddressModel Level { get; private set; }

        public static PlcAddressModel Cache { get; private set; }

        /// <summary>
        /// 皮带地址
        /// </summary>
        public static List<PlcAddressModel> Belt { get; private set; } = new List<PlcAddressModel>();

        /// <summary>
        /// 分拣气缸地址 集合
        /// </summary>
        public static List<PlcAddressModel> Cylinder { get; private set; } = new List<PlcAddressModel>();

        /// <summary>
        /// 下料仓地址 集合
        /// </summary>
        public static List<PlcAddressModel> Store { get; private set; } = new List<PlcAddressModel>();

        /// <summary>
        /// 滚筒地址 集合
        /// </summary>
        public static List<PlcAddressModel> Roller { get; private set; } = new List<PlcAddressModel>();

        /// <summary>
        /// 小车速度
        /// </summary>
        public static List<PlcAddressModel> Car { get; private set; } = new List<PlcAddressModel>();

        /// <summary>
        /// 通道最小时间
        /// </summary>
        public static List<PlcAddressModel> ChannelMin { get; private set; } = new List<PlcAddressModel>();

        /// <summary>
        /// 通道最大时间
        /// </summary>
        public static List<PlcAddressModel> ChannelMax { get; private set; } = new List<PlcAddressModel>();

        /// <summary>
        /// 其他参数
        /// </summary>
        public static List<PlcAddressModel> Other { get; private set; } = new List<PlcAddressModel>();

        /// <summary>
        /// 
        /// </summary>
        public static List<PlcAddressModel> System { get; private set; } = new List<PlcAddressModel>();

        public static bool Init()
        {
            try
            {
                var txt = Tool.ReadLine(ConfigFileInfo.PlcPortConfig);
                if (txt == null || txt.Count < 2)
                    return false;

                for(int i = 0; i < txt.Count; i++)
                {
                    var data = txt[i];
                    if (string.IsNullOrEmpty(data) == true)
                        continue;

                    var temp = MakeAddress(data);
                    if (temp == null)
                        return false;

                    if (data.Contains("启停") == true)
                        StartStop = temp;
                    else if (data.Contains("等级") == true)
                        Level = temp;
                    else if (data.Contains("传送带") == true)
                    {
                        temp.Index = Belt.Count + 1;
                        Belt.Add(temp);
                    }
                    else if (data.Contains("分拣气缸") == true)
                    {
                        temp.Index = Cylinder.Count + 1;
                        Cylinder.Add(temp);
                    }
                    else if (data.Contains("下料气缸") == true)
                    {
                        temp.Index = Store.Count + 1;
                        Store.Add(temp);
                    }
                    else if (data.Contains("滚筒") == true)
                    {
                        temp.Index = Roller.Count + 1;
                        Roller.Add(temp);
                    }
                    else if(data.Contains("缓存") == true)
                    {
                        Cache = temp;
                    }
                    else if (data.Contains("小车皮带速度"))
                    {
                        temp.Index = Car.Count + 1;
                        Car.Add(temp);
                    }
                    else if (data.Contains("通道时间范围下限"))
                    {
                        temp.Index = ChannelMin.Count + 1;
                        ChannelMin.Add(temp);
                    }
                    else if (data.Contains("通道时间范围上限"))
                    {
                        temp.Index = ChannelMax.Count + 1;
                        ChannelMax.Add(temp);
                    }
                    else if (data.Contains("其他参数"))
                    {
                        temp.Index = Other.Count + 1;
                        Other.Add(temp);
                    }
                    else if (data.Contains("系统参数"))
                    {
                        temp.Index = System.Count + 1;
                        System.Add(temp);
                    }
                };
                return true;
            }
            catch(Exception ex)
            {
                Logger.Error("初始化PLC地址异常", ex);
                return false;
            }
        }

        private static PlcAddressModel MakeAddress(string content)
        {
            try
            {
                var arr = content.Split();
                List<string> result = new List<string>();
                for(int i = 0; i < arr.Length; i++)
                {
                    if (string.IsNullOrEmpty(arr[i]) == false)
                        result.Add(arr[i]);
                }

                if (result.Count < 3)
                    return null;

                PlcAddressModel model = new PlcAddressModel();
                model.Title = result[0].Trim();
                model.AddressHex = result[1].Trim();
                model.AddressDec = int.Parse(result[2]);

                return model;
            }
            catch(Exception ex)
            {
                Logger.Error("解析plc地址异常", ex);
                return null;
            }
        }

        /*
通道时间范围下限1(ms)  D1310 1310
通道时间范围下限2(ms)  D1312 1312
通道时间范围下限3(ms)  D1314 1314
通道时间范围下限4(ms)  D1316 1316
通道时间范围下限5(ms)  D1318 1318
通道时间范围下限6(ms)  D1320 1320
通道时间范围下限7(ms)  D1322 1322
通道时间范围下限8(ms)  D1324 1324
通道时间范围下限9(ms)  D1326 1326

通道时间范围上限1(ms)  D1311 1311
通道时间范围上限2(ms)  D1313 1313
通道时间范围上限3(ms)  D1315 1315
通道时间范围上限4(ms)  D1317 1317
通道时间范围上限5(ms)  D1319 1319
通道时间范围上限6(ms)  D1321 1321
通道时间范围上限7(ms)  D1323 1323
通道时间范围上限8(ms)  D1325 1325
通道时间范围上限9(ms)  D1327 1327
        */

    }
}
